package com.cat.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cat.yygh.cmn.client.DictFeignClient;
import com.cat.yygh.common.util.JwtHelper;
import com.cat.yygh.model.user.Patient;
import com.cat.yygh.user.mapper.PatientMapper;
import com.cat.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 就诊人表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-11-20
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {
    /*注入字典模块*/
    @Autowired
    private DictFeignClient dictFeignClient;
    @Override
    public List<Patient> lsit(String token) {
        Long userId = JwtHelper.getUserId(token);
        QueryWrapper<Patient> wrapper = new QueryWrapper<Patient>();
        wrapper.eq("user_id",userId);
        List<Patient> patientList  = baseMapper.selectList(wrapper);
        //通过远程调用，得到编码对应具体内容，查询数据字典表内容
        patientList.stream().forEach(item ->{
            //封装其他参数
            this.packPatient(item);
        });
        return patientList ;
    }

    @Override
    public Patient getPatientId(Long id) {
        return this.packPatient(baseMapper.selectById(id));
    }

    //封装Patient其他参数
    private Patient packPatient(Patient item) {
        //获取证件对应名称
        String name = dictFeignClient.getNameByValue(Long.parseLong(item.getCertificatesType()));
        //省
        String province = dictFeignClient.getNameByValue(Long.parseLong(item.getProvinceCode()));
        //市
        String city = dictFeignClient.getNameByValue(Long.parseLong(item.getCityCode()));
        //区
        String district = dictFeignClient.getNameByValue(Long.parseLong(item.getDistrictCode()));

        item.getParam().put("certificatesTypeString",name);
        item.getParam().put("provinceString",province);
        item.getParam().put("cityString",city);
        item.getParam().put("districtString",district);
        item.getParam().put("fullAddress",province+city+district+item.getAddress());
        return item;
    }
}
